/*
 * @lc app=leetcode id=504 lang=cpp
 *
 * [504] Base 7
 *
 * https://leetcode.com/problems/base-7/description/
 *
 * algorithms
 * Easy (45.46%)
 * Likes:    179
 * Dislikes: 133
 * Total Accepted:    47.9K
 * Total Submissions: 105.3K
 * Testcase Example:  '100'
 *
 * Given an integer, return its base 7 string representation.
 * 
 * Example 1:
 * 
 * Input: 100
 * Output: "202"
 * 
 * 
 * 
 * Example 2:
 * 
 * Input: -7
 * Output: "-10"
 * 
 * 
 * 
 * Note:
 * The input will be in range of [-1e7, 1e7].
 * 
 */

// @lc code=start
class Solution {
public:
    string convertToBase7(int num) {
        string re;
        int flag = 0;
        if (num < 0){
            flag = 1;
            num = -num;
        }
        if (!num)
            re.push_back('0');
        while (num) {
            re.push_back(num%7 + '0');
            num /= 7;
        }
        if (flag)
            re.push_back('-');
        reverse(re.begin(), re.end());
        return re;
    }
};
// @lc code=end

